'use strict';

const Service = require('egg').Service;
const nodeMailer = require('nodemailer');

class MailerService extends Service {
  async send({ emails = [], html = '', subject = '' }) {
    const { app } = this;
    const { email } = app.config;
    const transporter = nodeMailer.createTransport({
      host: email.host,
      port: 587,
      secure: false,
      auth: {
        user: email.user,
        pass: email.pass,
      },
    });
    const mailOptions = {
      from: `bestlyg<${email.user}>`,
      to: emails.join(','),
      subject,
      html,
    };
    return await transporter.sendMail(mailOptions);
  }
}

module.exports = MailerService;
